package com.top.up.core.execption.handler;

import com.top.up.base.ResultBean;
import com.top.up.core.execption.CommonException;
import com.top.up.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice(basePackages = "com.top.up.controller")
public class GlobalExceptionHandler {

    /**
     * 自定义异常处理
     *
     * @param ex
     * @return
     * @throws Exception
     */
    @ExceptionHandler({CommonException.class})
    @ResponseBody
    public ResultBean<?> handlerCommonException(CommonException ex) throws Exception{
        log.error("CommonException msg: ", ex);
        return new ResultBean.Builder().result(ex.getCode(), ex.getMessage()).build();
    }

    /**
     * 通常异常处理
     *
     * @param ex
     * @return
     * @throws Exception
     */
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResultBean<?> handlerException(Exception ex) throws Exception{
        log.error("Exception msg: ", ex);
        return new ResultBean.Builder().result(ResultEnum.ERROR.getCode(), ex.getMessage()).build();
    }
}
